﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Vortex.Drawing {

	/// <summary>
	/// Device listener. Implements event based method of tracking device status
	/// </summary>
	public abstract class DeviceListener {
		private DeviceEventHandler _beforeDeviceResetHandler;
		private DeviceEventHandler _afterDeviceResetHandler;

		protected DeviceListener() {
			_beforeDeviceResetHandler = new DeviceEventHandler(OnBeforeDeviceReset);
			_afterDeviceResetHandler = new DeviceEventHandler(OnAfterDeviceReset);
		}

		/// <summary>
		/// Called before device reset.
		/// </summary>
		/// <param name="device">The device instance.</param>
		private void OnBeforeDeviceReset(DrawingDevice device) {
			BeforeDeviceReset();
		}

		/// <summary>
		/// Called after device reset.
		/// </summary>
		/// <param name="device">The device instance.</param>
		private void OnAfterDeviceReset(DrawingDevice device) {
			AfterDeviceReset();
		}

		/// <summary>
		/// Starts the listening specified device.
		/// </summary>
		/// <param name="device">The device instance.</param>
		protected void StartListeningDevice(DrawingDevice device) {
			device.BeforeReset += _beforeDeviceResetHandler;
			device.AfterReset += _afterDeviceResetHandler;
		}

		/// <summary>
		/// Stops the listening specified device.
		/// </summary>
		/// <param name="device">The device instance.</param>
		protected void StopListeningDevice(DrawingDevice device) {
			device.BeforeReset -= _beforeDeviceResetHandler;
			device.AfterReset -= _afterDeviceResetHandler;
		}

		/// <summary>
		/// Invoked before the device is reset.
		/// </summary>
		protected virtual void BeforeDeviceReset() { }

		/// <summary>
		/// Invoked after the device is reset successfully.
		/// </summary>
		protected virtual void AfterDeviceReset() { }
	}
}
